﻿package mortal.game.view.worship
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import baseEngine.modifiers.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.global.*;
    import mortal.component.rotate3dModelSprite.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.crossMainCity.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class WorshipModule extends BaseWindow
    {
        private var _bg:GImageBitmap;
        private var _rotate3d:Rotate3dModelSprite;
        private var _freeWorshipBtn:GLoadingButton;
        private var _goldWorshipBtn:GLoadingButton;
        private var _zhanliNumTxt:BitmapNumberText;
        private var _kingTxt:GTextFiled;
        private var _leftTimesTxt:GTextFiled;
        private var _costTxt:GTextFiled;
        private var _worshipTimesTxt:GTextFiled;
        private var _rewardTxt1:GTextFiled;
        private var _rewardTxt2:GTextFiled;
        private var _frameTimer:FrameTimer;
        private var _leftBtn:GLoadedButton;
        private var _rightBtn:GLoadedButton;
        protected var _rect3d:Rect3DObject;
        protected var _img2d:Img2D;
        protected var _bodyPlayer:ActionPlayer;
        protected var _roleModelPlayer:UserModelPlayer;
        protected var _testText:GTextFiled;
        private var _clickCount:int = 0;
        private var _isDelayArr:Array;
        private const _maxTimes:int = 3;
        private var turnValue:int;
        private var _effectPlayer:EffectPlayer;
        private var _effectPlayer2:EffectPlayer;
        private var _effectPlayer3:EffectPlayer;

        public function WorshipModule(param1:ILayer = null)
        {
            this._isDelayArr = [];
            super(param1);
            setSize(772 - 250, 580);
            title = Language.getString(90466);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        protected function onClickHandle(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.worship.info.adoreTimes;
            var _loc_3:* = WorshipConfig.instance.freeTimes;
            var _loc_4:* = _loc_3 - _loc_2;
            var _loc_5:* = getTimer();
            if (this._isDelayArr.length >= this._maxTimes)
            {
                if (_loc_5 - this._isDelayArr[0] <= 1000)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(90467));
                }
                this._isDelayArr.length = 0;
            }
            this._isDelayArr.push(_loc_5);
            if (this._freeWorshipBtn == event.target)
            {
                if (_loc_4 <= 0)
                {
                    MsgManager.showRollTipsMsg(Language.getString(90468));
                }
                else
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.worshipPlayRequest, 0));
                }
            }
            else if (this._goldWorshipBtn == event.target)
            {
                if (_loc_4 > 0)
                {
                    MsgManager.showRollTipsMsg(Language.getString(90469));
                }
                else
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.worshipPlayRequest, 1));
                }
            }
            return;
        }// end function

        protected function updateShow() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:TMainCityAdoreConfig = null;
            if (Cache.instance.worship.info)
            {
                this._worshipTimesTxt.text = String(Cache.instance.worship.info.kingBeAdoreTimes);
                _loc_1 = Cache.instance.worship.info.adoreTimes;
                _loc_2 = WorshipConfig.instance.freeTimes;
                _loc_3 = _loc_2 - _loc_1;
                if (_loc_3 < 0)
                {
                    _loc_3 = 0;
                }
                this._leftTimesTxt.text = Language.getStringByParam(90470, String(_loc_3));
                if (_loc_3 <= 0)
                {
                    _loc_4 = WorshipConfig.instance.getInfoByTimes((_loc_1 + 1));
                }
                else
                {
                    _loc_4 = WorshipConfig.instance.getInfoByTimes((_loc_1 + 1) + _loc_2);
                }
                if (_loc_4)
                {
                    this._costTxt.text = String(_loc_4.cost);
                }
                else
                {
                    this._costTxt.text = "";
                }
                this._testText.text = _loc_1.toString();
            }
            this.updateRewardTxt();
            return;
        }// end function

        protected function updateRewardTxt() : void
        {
            this._rewardTxt1.htmlText = Language.getStringByParam(90471, String(String(ZazenRewardConfig.instance.getInfoById(Cache.instance.role.entityInfo.level).adoreBindCoin)));
            this._rewardTxt2.htmlText = Language.getStringByParam(90471, String(String(ZazenRewardConfig.instance.getInfoById(Cache.instance.role.entityInfo.level).adoreUnbindCoin)));
            return;
        }// end function

        protected function setTxt(param1:SMiniPlayer) : void
        {
            var _loc_2:* = Cache.instance.mainCity.sMainCityPositons;
            this._kingTxt.htmlText = Language.getStringByParam(90472, _loc_2.guildName, NameUtil.getProxyName(param1.entityId), param1.name, CareerUtil.getCareerName(param1.career));
            this._zhanliNumTxt.text = String(param1.combat);
            return;
        }// end function

        private function setModel(param1:SMainCityPositon) : void
        {
            if (this._roleModelPlayer == null)
            {
                this._roleModelPlayer = FrObjectPool.getObject(RoleModelPlayer, this);
            }
            var _loc_2:* = param1.officer;
            var _loc_3:* = Cache.instance.role.roleEntityInfo;
            var _loc_4:* = MainCityCache.getPostionClothIdByType(param1.positon);
            this._roleModelPlayer = FrObjectPool.getObject(RoleModelPlayer, this);
            this._roleModelPlayer.updateInfo(_loc_2.career, _loc_2.sex, _loc_4, 0, _loc_3.hair, _loc_3.wing, _loc_2.VIP, _loc_3.isHideWing);
            this._roleModelPlayer.scaleAll = 2.2;
            this._roleModelPlayer.setRenderList(this._rect3d.renderList);
            this._roleModelPlayer.useLight();
            this._roleModelPlayer.rotationY = 0;
            this._rect3d.addObject3d(this._roleModelPlayer, 243 + 9, 465 - 88);
            this._rotate3d.model = this._roleModelPlayer;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.worshipBg1, 10, 39, null, this.onBgLoadCompl);
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.worshipTxt1, 10 + 168, 39 + 50, this.contentTopOf3DSprite));
            this._kingTxt = UIFactory.gTextField(Language.getString(90473), 60 + 15, 100 - 52, 400, 30, this.contentTopOf3DSprite, GlobalStyle.textFormatBai.center(), true);
            this._zhanliNumTxt = UIFactory.bitmapNumberText(300 - 55 + 24, 27 + 213 - 111 - 41, "PetGrowYellowNum.png", 18, 21, -2, this.contentTopOf3DSprite, 9);
            this._zhanliNumTxt.text = "0";
            this._worshipTimesTxt = UIFactory.gTextField("0", 180, 500 - 18, 300, 30, this.contentTopOf3DSprite, GlobalStyle.textFormatHuang2.setSize(16).center());
            this._freeWorshipBtn = UIFactory.gLoadingButton(ResFileConst.FreeWorshipBtn, 100, 500 + 24, 170, 39, this.contentTopOf3DSprite);
            this._freeWorshipBtn.configEventListener(MouseEvent.CLICK, this.onClickHandle);
            this._leftTimesTxt = UIFactory.gTextField("", 100 + 115, 100 + 409 + 23, 220, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatLv);
            this._leftTimesTxt.mouseEnabled = false;
            this._goldWorshipBtn = UIFactory.gLoadingButton(ResFileConst.GoldWorshipBtn, 300, 500 + 24, 170, 39, this.contentTopOf3DSprite);
            this._goldWorshipBtn.configEventListener(MouseEvent.CLICK, this.onClickHandle);
            this._costTxt = UIFactory.gTextField("5", 300 + 115 - 5, 100 + 409 + 23, 220, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatLv);
            this._costTxt.mouseEnabled = false;
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Yuanbao, 300 + 132 - 8, 300 + 210 + 24 - 2, this.contentTopOf3DSprite));
            this._rewardTxt1 = UIFactory.gTextField("", 100 - 86 - 92, 500 + 61, 300, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatBai.right(), true);
            this._rewardTxt2 = UIFactory.gTextField("", 300 - 86 - 92, 500 + 61, 300, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatBai.right(), true);
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Jinbi_bind, 100 + 132 - 8, 300 + 210 + 24 + 28, this.contentTopOf3DSprite));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Jinbi, 300 + 132 - 8, 300 + 210 + 24 + 28, this.contentTopOf3DSprite));
            this._leftBtn = UIFactory.gLoadedButton(ImagesConst.TurnLeft2_upSkin, 330 + 43 - 30 - 26, 483 - 86, 40, 36, this.contentTopOf3DSprite);
            this._leftBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onClickTurnBtn);
            this._rightBtn = UIFactory.gLoadedButton(ImagesConst.TurnRight2_upSkin, 141 + 30, 483 - 86, 40, 36, this.contentTopOf3DSprite);
            this._rightBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onClickTurnBtn);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopTurning);
            this._rotate3d = UIFactory.getUICompoment(Rotate3dModelSprite, 10, 39, this.contentTopOf3DSprite);
            this._rotate3d.setSize(513, 369);
            this._testText = UIFactory.gTextField("", 15, 400 + 141, 300, 20, this.contentTopOf3DSprite);
            this._testText.mouseEnabled = false;
            this._testText.visible = false;
            if (Global.isDebugModle)
            {
                this._testText.visible = true;
            }
            this.refresh();
            Dispatcher.dispatchEvent(new Event(EventName.WorshipInfoRequest));
            return;
        }// end function

        protected function onClickTurnBtn(event:MouseEvent) : void
        {
            if (this._roleModelPlayer)
            {
                if (event.currentTarget == this._leftBtn)
                {
                    this.turnValue = 2;
                }
                if (event.currentTarget == this._rightBtn)
                {
                    this.turnValue = -2;
                }
                this.start();
            }
            return;
        }// end function

        protected function stopTurning(event:MouseEvent = null) : void
        {
            if (this._frameTimer)
            {
                this._frameTimer.stop();
            }
            return;
        }// end function

        protected function onTurning(param1:FrameTimer) : void
        {
            if (this._roleModelPlayer)
            {
                this._roleModelPlayer.rotationY = this._roleModelPlayer.rotationY + this.turnValue;
            }
            return;
        }// end function

        private function start() : void
        {
            if (!this._frameTimer)
            {
                this._frameTimer = new FrameTimer(1, int.MAX_VALUE, true);
                this._frameTimer.addListener(TimerType.ENTERFRAME, this.onTurning);
            }
            this._frameTimer.start();
            return;
        }// end function

        protected function updateKingInfo(param1 = null) : void
        {
            var _loc_2:SMainCityPositons = null;
            var _loc_3:SMainCityPositon = null;
            var _loc_4:int = 0;
            var _loc_5:SMainCityPositon = null;
            if (this._rect3d)
            {
                _loc_2 = Cache.instance.mainCity.sMainCityPositons;
                if (_loc_2)
                {
                    _loc_4 = 0;
                    while (_loc_4 < _loc_2.positonInfos.length)
                    {
                        
                        _loc_5 = _loc_2.positonInfos[_loc_4] as SMainCityPositon;
                        if (_loc_5.positon == EMainCityPosition._EMainCityKing)
                        {
                            _loc_3 = _loc_5;
                            break;
                        }
                        _loc_4++;
                    }
                    this.setModel(_loc_3);
                    this.setTxt(_loc_3.officer);
                }
            }
            if (this._roleModelPlayer == null)
            {
                this._kingTxt.htmlText = Language.getString(90474);
            }
            return;
        }// end function

        private function playEffect(param1:EffectPlayer) : void
        {
            param1.play(PlayMode.ANIMATION_LOOP_MODE);
            return;
        }// end function

        private function playTransferEffect() : void
        {
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(this._effectPlayer);
                this._effectPlayer = EffectPlayerPool.instance.getEffectPlayer("GS-shanbi1", null, this._rect3d.renderList, false);
                this._effectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                FrEventDispatcher.instance.proxyAddEventListener(this._effectPlayer, Engine3dEventName.PLAYEND, this.playEndHander);
                this._rect3d.addObject3d(this._effectPlayer, 280, 220);
                this._effectPlayer2 = EffectPlayerPool.instance.getEffectPlayer("GS-shanbi1", null, this._rect3d.renderList, false);
                setTimeout(this.playEffect, 1000, this._effectPlayer2);
                FrEventDispatcher.instance.proxyAddEventListener(this._effectPlayer2, Engine3dEventName.PLAYEND, this.playEndHander);
                this._rect3d.addObject3d(this._effectPlayer2, 130, 350);
                this._effectPlayer3 = EffectPlayerPool.instance.getEffectPlayer("GS-shanbi1", null, this._rect3d.renderList, false);
                setTimeout(this.playEffect, 2000, this._effectPlayer3);
                FrEventDispatcher.instance.proxyAddEventListener(this._effectPlayer3, Engine3dEventName.PLAYEND, this.playEndHander);
                this._rect3d.addObject3d(this._effectPlayer3, 330, 320);
            }
            return;
        }// end function

        private function playEndHander(event:Event) : void
        {
            return;
        }// end function

        private function onBgLoadCompl() : void
        {
            if (isDisposed)
            {
                return;
            }
            NetDispatcher.addCmdListener(ServerCommand.MainCityPostionInfoGet, this.updateKingInfo);
            this.init3DRect();
            this.add3dBg();
            this.initModel();
            return;
        }// end function

        protected function init3DRect() : void
        {
            if (this._rect3d == null)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(10, 39, 513, 475), this);
                Rect3DManager.instance.windowShowHander(null, this);
            }
            GameProxy.guild.getMainCityPostionInfo();
            return;
        }// end function

        private function add3dBg() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._img2d);
                this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 513, 475), false);
                this._rect3d.addImg(this._img2d);
            }
            return;
        }// end function

        private function initModel() : void
        {
            return;
        }// end function

        public function refresh() : void
        {
            this.updateShow();
            return;
        }// end function

        private function remove3D() : void
        {
            if (this._rect3d)
            {
                if (this._img2d)
                {
                    this._rect3d.removeImg(this._img2d);
                    this._img2d = null;
                }
                if (this._bodyPlayer)
                {
                    this._rect3d.disposeObj3d(this._bodyPlayer);
                    this._bodyPlayer = null;
                }
                if (this._roleModelPlayer)
                {
                    this._rect3d.disposeObj3d(this._roleModelPlayer);
                    this._roleModelPlayer = null;
                }
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MainCityPostionInfoGet, this.updateKingInfo);
            this._bg.dispose(param1);
            this._bg = null;
            this._freeWorshipBtn.dispose(param1);
            this._freeWorshipBtn = null;
            this._goldWorshipBtn.dispose(param1);
            this._goldWorshipBtn = null;
            this._zhanliNumTxt.dispose(param1);
            this._zhanliNumTxt = null;
            this._kingTxt.dispose(param1);
            this._kingTxt = null;
            this._leftTimesTxt.dispose(param1);
            this._leftTimesTxt = null;
            this._costTxt.dispose(param1);
            this._costTxt = null;
            this._worshipTimesTxt.dispose(param1);
            this._worshipTimesTxt = null;
            this._rewardTxt1.dispose(param1);
            this._rewardTxt1 = null;
            this._rewardTxt2.dispose(param1);
            this._rewardTxt2 = null;
            if (this._frameTimer)
            {
                this._frameTimer.dispose(param1);
                this._frameTimer = null;
            }
            this._leftBtn.dispose(param1);
            this._leftBtn = null;
            this._rightBtn.dispose(param1);
            this._rightBtn = null;
            this._rotate3d.dispose();
            this._rotate3d = null;
            this.remove3D();
            super.disposeImpl();
            return;
        }// end function

    }
}
